<template>
     <el-dialog
    v-model="dialogVisible"
    title="批量导入"
    width="80%"
  >
  <div style="padding: 30px;">
    <div class="flex justify- items-center justify-end " v-if="pageName != 'trainingUserScore'"> 
      <el-button
          type="primary"
          text
          @click="downloadTem"
          :loading="exportLoading" 
        >
        <Icon icon="material-symbols-light:download"  />
        下载导入模版
      </el-button>
    </div>
    <div class="flex flex-col justify-center items-center mt-10">
      <el-date-picker
        v-if="pageName == 'attendanceMonthList'"
        v-model="monthDate"
        value-format="YYYY-MM"
        type="month"
        placeholder="选择月份"
      />
      <Icon  icon="vscode-icons:file-type-excel2" :size="80"/>
      <el-upload
    v-model:file-list="fileList"
    class="upload-demo"
    action=""
    :on-change="exportFile"
    :auto-upload="false"
       :show-file-list="true"
       accept=".xlsx, .xls"
  >
      <el-button class="mt-3">导入文件</el-button>
    </el-upload>
      <div v-if="pageName == 'trainingUserScore'" class="mt-5" style="color: #999999;">请先导出培训人员信息，补充成绩后，导入文件进行上传。支持格式:xs、xlsx</div>
      <div v-else class="mt-5" style="color: #999999;">根据模版填入信息后，导入文件进行上传。支持格式:xs、xlsx</div>
      <div class="el-upload__tip">
            <el-checkbox v-model="updateSupport" />
            是否更新已经存在的数据
          </div>
    </div>
  </div>
  <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import type { UploadUserFile } from 'element-plus'
import * as YearplanApi from '@/api/staffTrain/yearPlan'
import * as fileApi from '@/api/infra/file'
import download from '@/utils/download'
const dialogVisible = ref(false)
const fileList = ref<UploadUserFile[]>([])
/* 导入 */
const emits = defineEmits(['upload'])
const updateSupport = ref(false) // 是否更新已经存在的用户数据
const monthDate = ref('') // 选择月份
const fileObj = ref<UploadUserFile>()
const exportFile = (file: UploadUserFile) => {
    console.log(file)
    fileObj.value = file
}
let fileParams = reactive({
  fileName: ''
})
/* 打开 */
const pageName:any = ref('')
const open = async(name?: string) => {
    pageName.value = name
    dialogVisible.value = true
    updateSupport.value = false
    fileList.value = []
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
const exportLoading = ref(false)
const downloadTem = async () => {
  console.log('下载模版', pageName.value)
  let templateName = '';
  if( pageName.value=='yearPlan'){
    try {
      // 发起导出
      exportLoading.value = true
      const data = await YearplanApi.importYearplanApi()
      console.log(data,"文件流")
      download.excel(data, '导出模版.xls')
      return false;
    } catch {
    } finally {
      exportLoading.value = false
    }
  // 人员资质导出模板
  }else if (pageName.value.indexOf('qualifications') > -1) {
    // 人员资质公共导入模板
    templateName = 'certemp.xlsx';
    if (pageName.value == 'qualifications:15') {
      templateName = 'certemp15.xlsx';
    } else if (pageName.value == 'qualifications:16') {
      templateName = 'certemp16.xlsx';
    } else if (pageName.value == 'qualifications:21') {
      templateName = 'certemp21.xlsx';
    }
  // 安家费导出模版
  }else if (pageName.value=='settlementAllowance') {
    // 人员资质公共导入模板
    templateName = 'settlementAllowance.xls';
  // 导入购房补贴
  }else if (pageName.value=='housingAllowance') {
    // 人员资质公共导入模板
    templateName = 'housingAllowance.xls';
  // 导入月度考勤
  }else if (pageName.value=='attendanceMonthList') {
    // 人员资质公共导入模板
    templateName = 'attendanceMonth.xlsx';
  }else if (pageName.value=='quExport') {
    // 试题下载
    templateName = 'quImportTemplate.xlsx';
    // 员工花名册模版
  }else if (pageName.value=='userinfo') {
    templateName = 'userinfoTemplate.xls';
    // 护照信息
  }else if (pageName.value=='passport') {
    templateName = 'passportTemplate.xls';
    // 合同信息
  }else if (pageName.value=='userContract') {
    templateName = 'userContractTemplate.xls';
    // 荣誉信息
  }else if (pageName.value=='userHonor') {
    templateName = 'userHonorTemplate.xls';
  }
  if (templateName) {
    try {
      // 发起导出
      exportLoading.value = true
      fileParams.fileName = templateName;
      const data = await fileApi.downloadTemplate(templateName)
      console.log(data,"文件流")
      download.excel(data, templateName)
      return false;
    } catch {
    } finally {
      exportLoading.value = false
    }
  }

}
const submitForm = () => {
  fileObj.value.updateSupport =  updateSupport.value
  if (pageName.value == 'attendanceMonthList') {
    if (!monthDate.value) {
      alert('请选择导入月份！')
      return false;
    }
    fileObj.value.monthDate =  monthDate.value
  }
  emits('upload', fileObj.value)
  dialogVisible.value = false
}
</script>
<style lang="scss" scope></style>
